package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.commons.model.SystemProperties;
import cc.linker.steplink.crm.commons.service.UserCenterService;
import cc.linker.steplink.crm.domain.User;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.service.ISysConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.util.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/config")
public class UserCenterController {
    @Autowired
    private UserCenterService userCenterService;
    
    @Autowired
    private SystemProperties systemProperties;

    @Autowired
    private ISysConfig sysConfigService;

    @Autowired
    private HttpService httpService;

    @Value("${cors.allowOrgin}")
    private String synccontentUrl;

    @Value("${system.scrm.manageweb}")
    private String managewebUrl;
    @Autowired
    private UserInfoUtil userInfoUtil;


    /**
     * 选人地址
     * @return
     */
    @RequestMapping("/userselect")
    public Map<String, Object> getUserCenterRootPath(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String url = userCenterService.getSysUrl("UC-WP-UserSelect", request);
        Map<String, Object> result = new HashMap<>();
        if (!systemProperties.isMultiDomains()) {//非泛域名
            result.put("url", url);
            return result;
        }
        if (url.startsWith("http://")) {
            result.put("url", url.replace("http://", "http://" + userinfo.getOrgId() + "."));
        } else {
            result.put("url", url.replace("https://", "https://" + userinfo.getOrgId() + "."));
        }
        return result;
    }


    @PostMapping("/getwxtoken")
    public List getWxToken(){
        return sysConfigService.getWxTokenList();
    }


    /**
     * 创建短链接  接口地址写死了
     * @param url
     * @return
     * @throws GlobalException
     * @throws IOException
     * @throws URISyntaxException
     */
    @GetMapping("/getshorturl")
    public Map getShortUrl(@RequestParam(value = "url", defaultValue = "", required = true) String url) throws GlobalException, IOException, URISyntaxException {
        if("".equals(url)){
            throw GlobalException.build("参数错误");
        }
        String apiUrl = managewebUrl+"ShortUrl/api/ShortUrl/Create";
        Map<String, String> resData = new HashMap<String, String>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("Url", url);
        param.put("Expire", 10000);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        String getres = httpService.doGet(apiUrl, param, headers);
        if(null != getres){
            Map res = (Map) JSON.parse(getres);
            if("200".equals(res.get("Code").toString())){
                resData.put("shortUrl",res.get("Url").toString());
                return resData;
            }else{
                throw GlobalException.build("请求失败");
            }
        }else{
            throw GlobalException.build("请求失败");
        }
    }

}
